package com.qxgltj.qxgl.model;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.validator.constraints.Length;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import lombok.Data;

/**
 * Users
 */
@Data
@Entity
@GenericGenerator(name = "jpa-uuid",strategy = "uuid")
@JsonIgnoreProperties(value={"hibernateLazyInitializer","handler","fieldHandler"})
@EntityListeners(AuditingEntityListener.class)
public class Users {
    @Id
    @GeneratedValue(generator = "jpa-uuid") //主键生成机制  自增
    @Column(length = 32)
 private String id;//角色编号，uuid
 @Column(unique = true,nullable = false)
 private String  loginName;//登入名
 @NotNull
@Length(min = 6,max = 20)
 private String password;//密码
 @Column(name = "islockout",columnDefinition = "char(2) default '否'",insertable = false)
 private String islockout; //是否锁定 默认为否
 @CreationTimestamp
 @LastModifiedDate
 private Date lastlogintime;//最后一次登入时间
 @CreationTimestamp
 @CreatedDate
 @Column(updatable = false,nullable = false)
 private Date createtime;//账户的创立时间
 @Column(name = "psdwrongtime",columnDefinition = "int(10) default 0",insertable = false)
 private Integer psdwrongtime;//密码错误次数
 @CreationTimestamp//自动生成系统时间，格式为yyyy-MM-dd hh:MM:ss
 @CreatedDate
 private Date locktime;//被锁定时间
 @Email
 private String protectemail;//密保邮箱
 @Pattern(regexp = "^[1]([3-9])[0-9]{9}$")
 private String protectmtel;//密保手机号
 //用户角色相关
 @JsonIgnore
 @ManyToMany(cascade = CascadeType.REFRESH)
 @JoinTable(
     name = "role_users",
 inverseJoinColumns = @JoinColumn(name="role_id"),
 joinColumns = @JoinColumn(name = "user_id"))
 private List<Roles> rolesList;//用户操作角色时，就是操作这个字段
}